<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">产品维护</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="名称或编码" prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入SKU名称或编码检索"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="productList" @row-click="handleView" :default-sort="{prop: 'date', order: 'descending'}" border>
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="SKU编码" align="center" prop="specificationCode" :show-overflow-tooltip="true" width="150" sortable />
        <el-table-column label="图片" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <img v-if="scope.row.imgUrl" :src="scope.row.imgUrl" width="50" height="50" @mouseover="mouseOverImg(scope.row.imgUrl)" />
          </template>
        </el-table-column>
        <el-table-column label="SKU名称" align="center" prop="specificationName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="规格" align="center" prop="value1" :show-overflow-tooltip="true" width="150">
          <template slot-scope="scope">
            {{ scope.row.value1 == null ? '' : scope.row.value1 + scope.row.value2 }}
          </template>
        </el-table-column>
        <el-table-column label="简称" align="center" prop="shortName"  :show-overflow-tooltip="true" width="100" />
        <el-table-column label="助记码" align="center" prop="mnemonicCode" :show-overflow-tooltip="true" width="100" />
        <!-- <el-table-column label="成本核算" align="center" prop="costAccounting" :show-overflow-tooltip="true" width="100" /> -->
        <el-table-column label="产品分类" align="center" prop="productClassification" :show-overflow-tooltip="true" width="150" />
        <!-- <el-table-column label="有效天数" align="center" prop="validDate" :show-overflow-tooltip="true" width="100" /> -->
        <!-- <el-table-column label="品牌" align="center" prop="brandId" :show-overflow-tooltip="true" width="100" /> -->
        <!-- <el-table-column label="可组装/拆卸" align="center" prop="assembly" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.assembly == ''"></div>
            <div v-if="scope.row.assembly == '0'" style="color:#13C2C2">是</div>
            <div v-if="scope.row.assembly == '1'" style="color:#52C41A">否</div>
            <div v-else></div>
          </template>
        </el-table-column> -->
        <el-table-column label="单位" align="center" prop="unit" :show-overflow-tooltip="true" />
        <el-table-column label="是否启用扫码" align="center" prop="isSerial" width="100">
          <template slot-scope="scope">
            <div v-if="scope.row.isSerial == '0'" style="color:#52C41A">启用</div>
            <div v-if="scope.row.isSerial == '1'" style="color:#fd562c">禁用</div>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="approvalStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.approvalStatus == 0" style="color:#13C2C2">草稿</div>
            <div v-if="scope.row.approvalStatus == 1" style="color:#13C2C2">待审核</div>
            <div v-if="scope.row.approvalStatus == 2" style="color:#52C41A">已审核</div>
            <div v-if="scope.row.approvalStatus == 3" style="color:#fd562c">拒绝</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="productStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.productStatus == 0" style="color:#13C2C2">禁用</div>
            <div v-if="scope.row.productStatus == 1" style="color:#52C41A">正常</div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <el-dialog title="图片" :visible.sync="open" top="50px" width="500px" center>
      <img v-if="imgUrl" :src="imgUrl" width="100%" />
    </el-dialog>
  </div>
</template>

<script>
import { listProduct } from "@/api/system/basicdata/basicdata"
import { listProductList } from "@/api/system/basedata/productlist/productlist"
import { listBrand } from "@/api/system/basedata/brand/brand"
export default {
  name: "productList",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 列表数据
      productList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approvalStatus: 2,
        productStatus: 1,
        // 产品名称
        searchValue: undefined
      },
      open: false,
      imgUrl: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      listProduct(this.addDateRange(this.queryParams)).then(res => {
        this.productList = res.rows
        this.total = res.total
        this.loading = false
        this.getProductList()
        this.getBrand()
      })
    },
    // 获取产品分类
    getProductList(){
      listProductList().then(res => {
        this.productList.forEach(item => {
          let arr = res.rows.find(e =>{
            return e.id == item.productClassification
          })
          if(arr != undefined){
            item.productClassification = arr.sortName
          }
        })
      })
    },
    // 获取品牌
    getBrand(){
      listBrand(this.addDateRange({status: 0})).then(res => {
        this.productList.forEach(item => {
          let arr = res.rows.find(e => {
            return e.id == item.brandId
          })
          if(arr != undefined){
            item.brandId = arr.brandName
          }
        })
      })
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 查看产品图片
    mouseOverImg(e){
      this.open = true
      this.imgUrl = e
    },
    // 查看详情
    handleView(row) {
      if(this.$route.query.type == 'searchProduct'){
        if(this.$route.query.id){
          let data = this.$route.query.data
          data.productId = row.productSpecificationsId
          data.productName = row.specificationName
          data.productSpace = row.boxGauge
          this.$tab.closeOpenPage({ path: "/production/plan/edit", query: {
            id: this.$route.query.id,
            data: data
          }})
        }else{
          this.$tab.closeOpenPage({ path: "/production/plan/add", query: {
            productId: row.productSpecificationsId,
            productName: row.specificationName,
            productSpace: row.boxGauge
          }})
        }
      }else{
        this.$router.push({path:"/basicData/detail", query:{ data: row, type: 'product' }})
      }
    }
  }
}
</script>